package com.jiutong.meipenggang.ui.activitys;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.AppManager;
import com.jiutong.meipenggang.base.BaseActivity;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.BaseBean;
import com.jiutong.meipenggang.http.model.interfaces.RegisterAndLoginBiz;
import com.jiutong.meipenggang.http.present.UserInfoPresent;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.utils.TimerUtil;

import butterknife.Bind;
import butterknife.OnClick;

public class VerifyCodeActivity extends BaseActivity implements RegisterAndLoginBiz {


    @Bind(R.id.rl_common_title_back)
    RelativeLayout rlCommonTitleBack;
    @Bind(R.id.tv_common_title)
    TextView tvCommonTitle;
    @Bind(R.id.tv_verify_code_phone)
    TextView tvVerifyCodePhone;
    @Bind(R.id.et_forget_pwd_verify)
    EditText etForgetPwdVerify;
    @Bind(R.id.tv_forget_pwd_verify)
    TextView tvForgetPwdVerify;
    @Bind(R.id.btn_forget_pwd_submit)
    Button btnForgetPwdSubmit;

    private String phone;
    private UserInfoPresent userInfoPresent;


    private TimerUtil timerUtil;


    @Override
    public int getLayoutResouceId() {
        return R.layout.activity_verify_code;
    }

    @Override
    protected void initView() {
        super.initView();
        tvCommonTitle.setText("验证短信验证码");
        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            phone = SpConfig.getInstance().getString(Constants.USERNAME_STRING);
        } else {
            phone = bundle.getString("phone");
        }
        StringBuffer buffer = new StringBuffer(phone);
        tvVerifyCodePhone.setText(buffer.replace(3, 9, "******"));

        timerUtil = new TimerUtil(tvForgetPwdVerify);
        timerUtil.setCountTimer();
        timerUtil.startTimer();

        userInfoPresent = new UserInfoPresent(this, this);

    }


    @OnClick({R.id.rl_common_title_back, R.id.btn_forget_pwd_submit, R.id.tv_forget_pwd_verify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_common_title_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.btn_forget_pwd_submit:
                if (isRightAction()) {
                    userInfoPresent.checkPhoneCode();
                }
                break;
            case R.id.tv_forget_pwd_verify:
                userInfoPresent.getAuthCodeByFoget();
                break;
            default:
        }
    }

    private boolean isRightAction() {
        if (TextUtils.isEmpty(getVerificatCode())) {
            toastShortShow("请输入验证码");
            return false;
        }
        return true;
    }

    @Override
    public String getPhoneNum() {
        return phone;
    }

    @Override
    public String getVerificatCode() {
        return etForgetPwdVerify.getText().toString().trim();
    }

    @Override
    public String getOriPwd() {
        return null;
    }

    @Override
    public String getNewPwd() {
        return null;
    }

    @Override
    public void onVerCodeGetSuccess(BaseBean subjects) {
        if (subjects.getError() == -1) {
            timerUtil.startTimer();
            toastShortShow(subjects.getMsg());
        } else {
            toastShortShow(subjects.getMsg());
        }
    }

    @Override
    public void onSuccess(Object subjects) {
        BaseBean baseBean = (BaseBean) subjects;
        if (baseBean.getError() == -1) {
            toastShortShow(baseBean.getMsg());
            Bundle bundle = new Bundle();
            bundle.putString("phone",phone);
            openActivity(ReSetPwdActivity.class,bundle);
            AppManager.getAppManager().finishActivity(VerifyCodeActivity.this);
        } else {
            toastShortShow(baseBean.getMsg());
        }

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 6;
    }
}
